package com.chinaedu.oc.action.manageuser;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chinaedu.oc.context.Common.Session;
import com.chinaedu.oc.dao.manageuser.ManageUserDao;
import com.chinaedu.oc.entity.manageuser.ManageUser;
import com.chinaedu.oc.service.manageuser.ManageUserLoginService;
import com.chinaedu.oc.utils.ProcessResult;
import com.chinaedu.oc.utils.RandomValidataCode;
import com.chinaedu.oc.utils.StringUtils;


/**
 * @author lidan101
 *后台用户登录、修改密码、获取验证码
 */
@Controller
@RequestMapping("/login")
public class UserLoginAction {

	@Resource
	private ManageUserDao manageUserDao;
	@Resource
	private ManageUserLoginService manageUserLoginService;

	
	/**
	 * 登录接口
	 * @param request
	 * @param userName
	 * @param password
	 * @param checkcode
	 * @return
	 */
	@RequestMapping("doLogin")
	@ResponseBody
	public ProcessResult doLogin(HttpServletRequest request,
							@RequestParam("userName") String userName,
							@RequestParam("password") String password,
							@RequestParam("checkcode") String checkcode) {
		ProcessResult pr= new ProcessResult();
		HttpSession httpsession = request.getSession();
		String checkcode_session = (String) httpsession.getAttribute("CHECKCODE");//获取验证码后就写进session中
		// 防止用户刷新重复提交,重定向带参数
		if (StringUtils.isBlank(userName) || StringUtils.isBlank(password)) {
			pr.setStatus(false);
			pr.setMsg("用户名或密码为空");
			return pr;
		}
		if (StringUtils.isBlank(checkcode)) {
			pr.setStatus(false);
			pr.setMsg("验证码为空");
			return pr;
		}		
		if (!checkcode.equalsIgnoreCase(checkcode_session)) {
			pr.setStatus(false);
			pr.setMsg("验证码输入不正确");
			return pr;
		}
		// 用户名是否存在
		ManageUser manageUser = manageUserDao.findByUsername(userName);
		if (manageUser == null) {
			pr.setStatus(false);
			pr.setMsg("该用户不存在");
			return pr;
		}
		if ("0".equals(manageUser.getStatus())) {
			pr.setStatus(false);
			pr.setMsg("该用户已停用");
			return pr;
		}
		if (!password.equals(manageUser.getPassword())) {
			pr.setStatus(false);
			pr.setMsg("密码输入有误");
			return pr;
		}
		//写入session
		manageUserLoginService.writeManageUserLogin(manageUser, request);
		pr.setData(manageUser);
		pr.setStatus(true);
		return pr;
	}
	/**
	 * 获取验证码
	 *
	 * @param request
	 * @param response
	 */
	@RequestMapping("/checkcode")
	public void getcheckcode(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("image/jpeg");// 设置相应类型,告诉浏览器输出的内容为图片
		response.setHeader("Pragma", "No-cache");// 设置响应头信息，告诉浏览器不要缓存此内容
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expire", 0);
		RandomValidataCode randomValidateCode = new RandomValidataCode();
		try {
			randomValidateCode.getRandcode(request, response);// 输出图片方法
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	/**
	 *  根据用户名  从session 中获取用户信息
	 * @param userName
	 * @return
	 */
	@RequestMapping("getUserInfo")
	@ResponseBody
	public ProcessResult getUserInfo(HttpServletRequest request,@RequestParam("userName") String userName) {
		ProcessResult pr= new ProcessResult();
		HttpSession session = request.getSession();
		ManageUser manageuser = (ManageUser) session.getAttribute(Session.manageUserLogin.name());
		if(manageuser==null){
			pr.setStatus(false);
			pr.setMsg("用户不存在");
		}else{
			pr.setData(manageuser);
			pr.setStatus(true);
		}
		
		return pr;
	}
	
	/**
	 * 退出
	 * @param session
	 * @return
	 */
	@RequestMapping("logout")
	@ResponseBody
	public ProcessResult logout(HttpSession session) {
		ProcessResult pr= new ProcessResult();
		manageUserLoginService.logout(session);
		pr.setStatus(true);
		return pr;
	}

}
